module Display {
	export class BasicContainer extends egret.DisplayObjectContainer {
		private _txInfo: egret.TextField;
		private leftTF: egret.TextField;
		private rightTF: egret.TextField;

		/**
		 * 构造函数
		 * 监听添加到舞台事件
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddedToStage,this);
		}

		/**
		 * 添加到舞台监听器
		 * 加载图片 
		 * @param evt:Event
		 */
		private onAddedToStage(evt:egret.Event):void{
			var imgLoader:egret.ImageLoader=new egret.ImageLoader;
			imgLoader.once(egret.Event.COMPLETE,this.imgLoaderHandler,this);
			imgLoader.load("resource/hero.png");
		}

		/**
		 * 图片加载监听器
		 * @param evt:Event
		 */
		private imgLoaderHandler(evt: egret.Event): void {
			//初始化女孩
			var girl:egret.Bitmap=new egret.Bitmap(evt.currentTarget.data);
			girl.x=this.stage.stageWidth/2-girl.width/2;
			girl.y=this.stage.stageHeight/2+100;
			this.addChild(girl);
			girl.touchEnabled=false;
			/*** 按钮生成代码 ***/
			this.leftTF = new egret.TextField;
			this.leftTF.size = 28;
			this.leftTF.textAlign = egret.HorizontalAlign.CENTER;
			this.leftTF.textColor = 0xffffff;
			this.leftTF.background = true;
			this.leftTF.backgroundColor = 0xd71345;
			this.leftTF.text = "红色容器";
			this.leftTF.x = this.stage.stageWidth / 4 - this.leftTF.width / 2;
			this.leftTF.y = 220;
			this.leftTF.touchEnabled = true;
			this.addChild(this.leftTF);

			this.rightTF = new egret.TextField;
			this.rightTF.size = 28;
			this.rightTF.textAlign = egret.HorizontalAlign.CENTER;
			this.rightTF.textColor = 0xffffff;
			this.rightTF.background = true;
			this.rightTF.backgroundColor = 0x102b6a;
			this.rightTF.text = "蓝色容器";
			this.rightTF.x = this.stage.stageWidth / 2 + this.stage.stageWidth / 4 - this.rightTF.width / 2;
			this.rightTF.y = 220;
			this.rightTF.touchEnabled = true;
			this.addChild(this.rightTF);
			//初始化两个容器
			var leftCon = new egret.DisplayObjectContainer;
			var leftCage = new egret.Shape();
			leftCage.graphics.lineStyle(10, 0xd71345, 1, true)
			leftCage.graphics.lineTo(0, 0);
			leftCage.graphics.lineTo(250, 0);
			leftCage.graphics.lineTo(250, 250);
			leftCage.graphics.lineTo(0, 250);
			leftCage.graphics.lineTo(0, 0);
			leftCon.addChild(leftCage);
			leftCon.x = this.stage.stageWidth / 4 - leftCon.width / 2;
			leftCon.y = 280;
			this.addChild(leftCon);
			var rightCon = new egret.DisplayObjectContainer;
			var rightCage = new egret.Shape();
			rightCage.graphics.lineStyle(10, 0x102b6a, 1, true)
			rightCage.graphics.lineTo(0, 0);
			rightCage.graphics.lineTo(250, 0);
			rightCage.graphics.lineTo(250, 250);
			rightCage.graphics.lineTo(0, 250);
			rightCage.graphics.lineTo(0, 0);
			rightCage.graphics.endFill();
			rightCon.addChild(rightCage);
			rightCon.x = this.stage.stageWidth / 2 + this.stage.stageWidth / 4 - rightCon.width / 2;
			rightCon.y = 280;
			this.addChild(rightCon);
			/// 提示信息
			this._txInfo = new egret.TextField;
			this._txInfo.size = 28;
			this._txInfo.width = 550;
			this._txInfo.textAlign = egret.HorizontalAlign.CENTER;
			this._txInfo.textColor = 0x000000;
			this._txInfo.lineSpacing = 6;
			this._txInfo.multiline = true;
			this._txInfo.text = "点击不同颜色按钮，将女孩放到不同的容器中，拖动容器女孩随着容器移动";
			this._txInfo.x = this.stage.stageWidth / 2 - this._txInfo.width / 2;
			this._txInfo.y = 120;
			this.addChild(this._txInfo);
			//为左按钮添加点击事件
			this.leftTF.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
				if(this.getChildIndex(girl)!=-1){
					this.removeChild(girl);
				}else if(rightCon.getChildIndex(girl)!=-1){
					rightCon.removeChild(girl);
				}
				leftCon.addChild(girl);
				girl.x=leftCage.width/2-girl.width/2;
				girl.y=leftCage.height/2-girl.height/2;
				this.leftTF.touchEnabled=false;
				this.rightTF.touchEnabled=true;
				leftCon.touchEnabled=true;
				rightCon.touchEnabled=false;
			},this);
			//为右按钮添加点击事件
			this.rightTF.addEventListener(egret.TouchEvent.TOUCH_TAP,(evt:egret.TouchEvent)=>{
				if(this.getChildIndex(girl)!=-1){
					this.removeChild(girl);
				}else if(leftCon.getChildIndex(girl)!=-1){
					leftCon.removeChild(girl);
				}
				rightCon.addChild(girl);
				girl.x=rightCage.width/2-girl.width/2;
				girl.y=rightCage.height/2-girl.height/2;
				this.leftTF.touchEnabled=true;
				this.rightTF.touchEnabled=false;
				leftCon.touchEnabled=false;
				rightCon.touchEnabled=true;
			},this);
			//为左容器添加拖动事件
			leftCon.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
			//为右窗口添加拖动事件
			rightCon.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
			//为精灵添加拖动事件
			//girl.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
		}

		private sx:number;
		private sy:number;
		/**
		 * 开始触摸
		 */
		private onTouchBegin(evt:egret.TouchEvent):void{
			evt.stopImmediatePropagation();
			var dispatcher:egret.EventDispatcher=<egret.EventDispatcher>evt.currentTarget;
			dispatcher.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
			dispatcher.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.onTouchMove,this);
			dispatcher.addEventListener(egret.TouchEvent.TOUCH_END,this.onTouchEnd,this);
			this.sx=evt.stageX;
			this.sy=evt.stageY;
		}

		/**
		 * 触摸移动
		 */
		private onTouchMove(evt:egret.TouchEvent):void{
			evt.stopImmediatePropagation();
			var vx:number=evt.stageX-this.sx;
			var vy:number=evt.stageY-this.sy;
			this.sx=evt.stageX;
			this.sy=evt.stageY;
			var displayObj:egret.DisplayObject=evt.currentTarget;
			displayObj.x+=vx;
			displayObj.y+=vy;
		}

		/**
		 * 触摸结束
		 */
		private onTouchEnd(evt:egret.TouchEvent):void{
			evt.stopImmediatePropagation();
			var dispatcher:egret.EventDispatcher=<egret.EventDispatcher>evt.currentTarget;
			dispatcher.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.onTouchMove,this);
			dispatcher.removeEventListener(egret.TouchEvent.TOUCH_END,this.onTouchEnd,this);
			dispatcher.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.onTouchBegin,this);
		}
	}
}